/**
 * this function fix the bug that the Calendar can not show the
 *  cross date event by split one cross date event to many non
 *  cross date event
 */
export function splitCrossDateEvent(eventList){
    let newEventList=new Array()
    for(let i=0;i<eventList.length;i++) {
        let event=eventList[i];
        let allDate=getAllDate(event.start,event.end);
        if(allDate.length==1){
            event.originId=event.id;
            event.originStart= event.start;
            event.originEnd=event.end;
            newEventList.push(event);

        }else {
            for(let i=0;i<allDate.length;i++){
                let date=allDate[i];
                if(i==0){
                    let newEvent={
                        editAble: event.editAble,
                        end: date+' 23:59:00',
                        id: event.id,
                        memo: event.memo,
                        simpleUserList: event.simpleUserList,
                        start: event.start,
                        title: event.title,
                        originId: event.id,
                        originStart: event.start,
                        originEnd: event.end,
                        remindTime:event.remindTime,
                        remindTimeName:event.remindTimeName
                    };
                    newEventList.push(newEvent);
                }else if(i==allDate.length-1){
                    let newEvent={
                        editAble: event.editAble,
                        end: date+' 00:00:00',
                        id: event.id+'_'+i,
                        memo: event.memo,
                        simpleUserList: event.simpleUserList,
                        start: event.end,
                        title: event.title,
                        originId: event.id,
                        originStart: event.start,
                        originEnd: event.end,
                        remindTime:event.remindTime,
                        remindTimeName:event.remindTimeName
                    };
                    if(newEvent.start!==newEvent.end){
                        newEventList.push(newEvent);
                    }

                }else{
                    let newEvent={
                        editAble: event.editAble,
                        end: date+' 23:59:00',
                        id: event.id+'_'+i,
                        memo: event.memo,
                        simpleUserList: event.simpleUserList,
                        start: date+' 00:00:00',
                        title: event.title,
                        originId: event.id,
                        originStart: event.start,
                        originEnd: event.end,
                        remindTime:event.remindTime,
                        remindTimeName:event.remindTimeName
                    };
                    newEventList.push(newEvent);
                }
            }
        }
    }
    return newEventList;
}

function getAllDate(start, end){
    start=start.substring(0,10);
    end=end.substring(0,10);
    //初始化日期列表，数组
    let allDateArray = new Array();
    let i=0;
    //开始日期小于等于结束日期,并循环
    while(start<=end){
        allDateArray[i] = start;
        //获取开始日期时间戳
        let startTimeSt = new Date(start).getTime();
        //增加一天时间戳后的日期
        let nextDate = startTimeSt + (24*60*60*1000);
        //拼接年月日，这里的月份会返回（0-11），所以要+1
        let nextDateY = new Date(nextDate).getFullYear()+'-';
        let nextDateM = (new Date(nextDate).getMonth()+1 < 10)?'0'+(new Date(nextDate).getMonth()+1)+'-':(new Date(nextDate).getMonth()+1)+'-';
        let nextDateD = (new Date(nextDate).getDate() < 10)?'0'+new Date(nextDate).getDate():new Date(nextDate).getDate();
        start = nextDateY+nextDateM+nextDateD;
        i++;
    }
    return allDateArray;
}

